﻿using Abp.Application.Services.Dto;
using Abp.AutoMapper;
using Abp.Domain.Repositories;
using Abp.Extensions;
using Abp.Linq.Extensions;
using Akso.Authorization;
using Akso.Courses.Dto;
using Akso.Entities;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Diagnostics;
using System.Linq;
using System.Linq.Dynamic;
using System.Text;
using System.Threading.Tasks;
using Akso.Dto;
using Abp.Authorization;

namespace Akso.Courses
{
    public class CourseAppService: AbpZeroTemplateAppServiceBase, ICourseAppService
    {
        private readonly IRepository<CourseEntity, Guid> _coursesRepository;

        public CourseAppService(IRepository<CourseEntity, Guid> coursesRepository)
        {
            _coursesRepository = coursesRepository;

        }
        #region 查询数据
        /// <summary>
        /// 根据输入条件进行数据查询
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        private IQueryable<CourseEntity> getQuery(GetCoursesInput input)
        {
            var query = _coursesRepository.GetAll()
                .WhereIf(
                    !input.Filter.IsNullOrWhiteSpace(),
                    t =>
                        t.CourseName.Contains(input.Filter) ||
                        t.Remark.Contains(input.Filter)
                );
            return query;
        }
        public async Task<ListResultDto<CourseListDto>> GetObjects(GetCoursesInput input)
        {
            var query = getQuery(input);
            var dataCount = await query.CountAsync();
            var data = await query.OrderBy(input.Sorting).PageBy(input).ToListAsync();

            return new PagedResultDto<CourseListDto>(
                dataCount,
                data.MapTo<List<CourseListDto>>()
                );
        }
        #endregion

        #region 删除数据，带权限检查
        /// <summary>
        /// 删除对象，带权限检查。
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [AbpAuthorize(AppPermissions.Pages_EduAdminModule_Courses_Delete)]
        public async Task DeleteObject(EntityDto<Guid> input)
        {
            var o = await _coursesRepository.GetAsync(input.Id);
            await _coursesRepository.DeleteAsync(o);
        }
        #endregion

        #region 新增、编辑对象，带权限检查
        /// <summary>
        /// 新增、编辑对象，带权限检查。
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [AbpAuthorize(AppPermissions.Pages_EduAdminModule_Courses_Create, AppPermissions.Pages_EduAdminModule_Courses_Edit)]
        public async Task<CourseEntity> CreateOrUpdateObject(CreateOrUpdateCourseInput input)
        {
            if (input.Course.Id.HasValue)
            {
                return await UpdateObject(input);
            }
            else
            {
                return await CreateObject(input);
            }
        }

        /// <summary>
        /// 新增对象，带权限检查。
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [AbpAuthorize(AppPermissions.Pages_EduAdminModule_Courses_Create)]
        protected virtual async Task<CourseEntity> CreateObject(CreateOrUpdateCourseInput input)
        {
            var o = input.Course.MapTo<CourseEntity>();
            if (AbpSession.TenantId != null)
                o.TenantId = (int)AbpSession.TenantId;
            else
                o.TenantId = 1;
            //检查名称是否存在

            return await _coursesRepository.InsertAsync(o);
        }
        /// <summary>
        /// 编辑对象，带权限检查。
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [AbpAuthorize(AppPermissions.Pages_EduAdminModule_Courses_Edit)]
        protected virtual async Task<CourseEntity> UpdateObject(CreateOrUpdateCourseInput input)
        {
            Debug.Assert(input.Course.Id != null, "input.Classroom.Id should be set.");

            var o = await _coursesRepository.GetAsync(input.Course.Id.Value);
            //Update classroom properties
            input.Course.MapTo(o);
            return await _coursesRepository.UpdateAsync(o);
        }
        #endregion

        #region 根据Guid获取数据对象
        /// <summary>
        /// 新增、编辑页面中获取对象，带权限检查。
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [AbpAuthorize(AppPermissions.Pages_EduAdminModule_Courses_Create, AppPermissions.Pages_EduAdminModule_Courses_Edit)]
        public async Task<GetCourseForEditOutput> GetObjectForEdit(NullableIdDto<Guid> input)
        {
            var output = new GetCourseForEditOutput
            {

            };

            if (!input.Id.HasValue)
            {
                //Creating a new object
                output.Course = new CourseEditDto();
            }
            else
            {
                //Editing an existing object
                var existingObject = await _coursesRepository.GetAsync(input.Id.Value);

                output.Course = existingObject.MapTo<CourseEditDto>();
            }

            return output;
        }
        #endregion
    }
}
